/*----------------------------------------------------
*  This file is common part
-----------------------------------------------------*/
$(function(){
	$.common.goTop();
	
//	$("#signIn").fancybox({
//		'transitionIn'		: 'none',
//		'autoScale'			: false,
//		'transitionOut'		: 'none',
//	});
	
	$('#refresh').click(function(){
		$.common.refresh($(this));
	});
	$("#profile").click(function() {
		$("form#profileForm").submit();
	});
});

$.common = {
	//change menu background if it's active
	menuBgChange: function(id) {
		$('.item-' + id).addClass('active');
	},
	archiveToggle: function() {
		$('a[id^="archive-par-"]').click (
			function() {
				var id = $(this).attr('id').replace(/[a-zA-Z\-]*/, '');
				if ($(this).hasClass('expand')) {
					$(this).attr('title', $(this).attr('title').replace('collapse', 'expand'));
					$('#archive-sub-'+ id).slideUp();
				} else {
					$(this).attr('title', $(this).attr('title').replace('expand', 'collapse'));
					$('#archive-sub-'+ id).slideDown();
				}
				$(this).toggleClass('collapse').toggleClass('expand');
				id = null;
			}
		);
	},
	goTop: function() {
		var obj = $('#toTop');
		$(window).scroll(function () {
			if($.browser.msie && $.browser.version == '6.0'){
				obj.css('top', $(this).scrollTop() + $(this).height() - 170);
			}
			$(window).scrollTop()=="0"?obj.fadeOut("slow"):obj.fadeIn("slow");
		});
		obj.live("click", function(){
			$("html,body").animate({scrollTop:0},350);return false;
		});
	},
	/** validation **/
	test: function(reg, value) {
		return reg.test(value);
	},
	blank: function(value) {
		return null === value || '' === value;
	},
	digit: function(value) {
		return /^\d+$/.test(value);
	},
	email: function(value) {
		return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);
	},
	url: function(value) {
		return /^(https?):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value);
	},
	/** ajax **/
	post: function(url, params, fn) {
		$.post(
			BASE + url,
			params,
			function(data) {
				fn(data);
			},
			"json"
		);
	},
	ajax: function(url, params, fn, async) {
		$.ajax({
			type: "post",
			url: BASE + url,
			data: params,
			async: async && true,
			dataType: "json",
			success: function(data) {
				fn(data);
			}
		});
	},
	ajaxForm : function(b, a, c) {
		b.ajaxForm({
			type : "post",
			dataType : "json",
			beforeSubmit : a,
			success : c
		});
	},
	btnStatus: function(id, b, v) {
		$('#' + id).attr('disabled',b).val(v);
	},
	refresh: function(o) {
		o.find("img").attr('src', BASE + '/captcha?time=' + new Date().getTime());
	}
};

/*---- Error Msg ------------------------------------*/
;
(function($) {
	$.cde = {};
	$.cde.msg = {
		DB_001 : '<b>Database</b> error, please try later',
		CO_001 : '<b>Email</b> should be right format',
		CO_002 : '<b>Website</b> should be right format',
		CO_003 : '<b>Captcha</b> is incorrect',
		CP_001 : '<b>Captcha</b> should be only 4 chars',
		
		AC_001 : '<b>Username</b> or <b>Password</b> incorrect',
		AC_002 : '<b>Nick Name</b> should be [2-15] chars',
		AC_003 : '<b>Account Name</b> should be [2-15] chars',
		AC_004 : '<b>Account Name</b> is exist',
		AC_005 : '<b>Password</b> should be [6-18] chars',
		AC_006 : '<b>Photo</b> should not be null, pls select one',
		AC_007 : '<b>Email</b> is exist',
		AC_008 : '<b>Account</b> is invalid, pls activate',
		AC_009 : '<b>Email</b> is not exist',
		AC_010 : '<b>Account Name</b> is not exist',
		AC_011 : '<b>Account Aready activated!</b> no need activate again',
		AC_012 : '<b>Email</b> or <b>Account</b> incorrect',
		AC_013 : '<b>Activate Code</b> invalid, pls request again',
		AC_014 : '<b>Account</b> was locked, contact admin',
		AC_015 : "<b>Old Password</b> should be [6-18] chars",
		AC_016 : "<b>Old Password</b> incorrect",
		AC_017 : "<b>New Password</b> should be [6-18] chars",
		AC_018 : "<b>New Password</b> and <b>Old Password</b> could not be same",
		
		AB_001 : '<b>Confirm Password</b> should be [6-18] chars',
		AB_002 : '<b>Twice password</b> should be same',
			
		BL_CO_001 : '<b>Name</b> should be [2-15] chars',
		BL_CO_002 : '<b>Comment</b> should be [>=5] chars'
	};
})(jQuery);